home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '92 / Hacks ’92 / FinderMenu ƒ / <Stuff> / FinderStuff.c next >
Encoding:
C/C++ Source or Header  |  1992-06-14  |  2.3 KB  |  122 lines  |  [TEXT/KAHL]

  1. /*
  2.  * (C) 1992 SixxHeads Software
  3.  * (C) 1992 Berkeley Systems Inc.
  4.  * 
  5.  * This code is freely distributable, but credit must be given in any
  6.  * derivative work.
  7.  * 
  8.  * <Revision History>
  9.  *        04/28/92 smz Created.
  10.  */
  11.  
  12. #include <FinderStuff.h>
  13.  
  14. typedef struct {
  15.     short            visible;
  16.     short            count;
  17.     short            unkown0;
  18.     short            menuID;
  19.     short            unkown1;
  20.     short            unkown2;
  21.     unsigned char    title[];
  22.     /* perItem array */
  23. } *fMenuPtr, **fMenuHandle;
  24.  
  25. typedef struct {
  26.     unsigned long    aEvt;
  27.     char            unknown0;
  28.     char            unknown1;
  29.     char            cmdKey;
  30.     char            unused;
  31.     unsigned char    itemText[];
  32. } *perItemPtr;
  33.  
  34. fMenuHandle hMenu;
  35.  
  36. static short UseFinderResfile(void)
  37. {
  38.     short vRefNum;
  39.     long dirID;
  40.     
  41.     if (FindFolder(kOnSystemDisk, kSystemFolderType, true, &vRefNum, &dirID) != noErr) {
  42. #ifdef DEBUG
  43.         DebugStr("\pcan't locate system folder!");
  44. #endif
  45.         return -1;
  46.     }
  47.     return HOpenResFile(vRefNum, dirID, FinderName, fsRdPerm);
  48. }
  49.  
  50. static Boolean TryFindItem(fMenuHandle hfmnu, unsigned long aeID, unsigned long *cmdItem)
  51. {
  52.     reg fMenuPtr p = *hfmnu;
  53.     reg unsigned char *t;
  54.     reg perItemPtr item;
  55.     short ctItems = p->count;
  56.     short i;
  57.  
  58.     *cmdItem = (((long) (p->menuID - 0x3e3)) << 16);
  59.     
  60.     t = p->title;
  61.  
  62.     for (i = 0;  i < ctItems;  i++) {
  63.         if ((*t & 0x01) == 0)    /* even, pad */
  64.             t += *t + 2;
  65.         else
  66.             t += *t + 1;
  67.         item = (perItemPtr) t;
  68.         if (item->aEvt == aeID) {
  69.             *cmdItem |= (i + 1);
  70.             return true;
  71.         }
  72.         t = item->itemText;
  73.     }
  74.     return false;
  75. }
  76.  
  77. void GetFinderItem(unsigned long aeID, short hintID, unsigned long *cmdItem)
  78. {
  79.     short ctResources;
  80.     fMenuHandle hfmnu;
  81.     short i;
  82.     short finderRefNum = UseFinderResfile();
  83.     
  84.     if (finderRefNum == 0) {
  85.         *cmdItem = 0;
  86.         return;
  87.     }
  88.     UseResFile(finderRefNum);
  89.  
  90.     /* try to use the hint item first */
  91.     if (hintID != -1) {
  92.         hfmnu = (fMenuHandle) GetResource('fmnu', hintID);
  93.         if (hfmnu == nil) {
  94. #ifdef DEBUG
  95.             DebugStr("\pCouldn't get 'fmnu' resource #%d!");
  96. #endif
  97.         } else
  98.             if (TryFindItem(hfmnu, aeID, cmdItem))
  99.                 goto exit;
  100.     }
  101.     
  102.     ctResources = CountResources('fmnu');
  103.     
  104.     for (i = 1;  i <= ctResources;  i++) {
  105.         hfmnu = (fMenuHandle) GetIndResource('fmnu', i);
  106.         if (hfmnu == nil) {
  107. #ifdef DEBUG
  108.             DebugStr("Couldn't get 'fmnu' resource!");
  109. #endif
  110.             break;
  111.         }
  112.         if (TryFindItem(hfmnu, aeID, cmdItem))
  113.             goto exit;
  114.     }
  115.  
  116.     *cmdItem = 0;    /* not found */
  117.  
  118. exit:
  119.     CloseResFile(finderRefNum);
  120. }
  121.  
  122.